import {Component, OnInit} from '@angular/core';
import {ExtendHttpService} from "../../../../common/extend-http.service";
import {EssService} from "../../ess.service";
import {ActivatedRoute, Router} from "@angular/router";
import {TipService} from "../../../services/tip.service";
import {debounceTime} from "rxjs/operator/debounceTime";
import {Observable} from "rxjs/Observable";

@Component({
  selector: 'app-inventory-in',
  templateUrl: './inventory-in.component.html',
  styleUrls: ['./inventory-in.component.css']
})
export class InventoryInComponent implements OnInit {

  tipType = 'error';

  v: any = '';

  editInfo: any = {};

  constructor(
    private ext: ExtendHttpService,
    private ess: EssService,
    private route: ActivatedRoute,
    private tips: TipService,
    private router: Router,
  ) {
  }

  ngOnInit() {
    this.v = JSON.parse(this.route.queryParams['value']['obj']);
  }

  save() {
    if (!this.editInfo['ms.unit']) {
      this.tipType = 'error';
      this.tips.change(true, '请选择单位');
      return;
    }
    if (!this.editInfo['ms.num']) {
      this.tipType = 'error';
      this.tips.change(true, '请填写数量');
      return;
    }
    if (!this.editInfo['price']) {
      this.tipType = 'error';
      this.tips.change(true, '请填写总价');
      return;
    }
    if (this.editInfo['price'] <= 0) {
      this.tipType = 'error';
      this.tips.change(true, '总价应大于0');
      return;
    }
    if (!this.editInfo['instockname']) {
      this.tipType = 'error';
      this.tips.change(true, '请填写入库人');
      return;
    }
    // 按钮【按钮一】的回调
    let params = Object.assign(this.editInfo, {
      'ms.materiel_id': this.v.materiel_id,
      'ms.name': this.v.name,
    });
    this.ext.get('/merchantApp/inventory/stock/warehousing', params).subscribe(res => {
      if (res.json().code === '0') {
        this.tipType = 'success';
        this.tips.change(true, res.json().message);
        setTimeout(() => {
          this.router.navigate(['/main/ess/inventory']);
        }, 500);
      } else {
        this.tipType = 'error';
        this.tips.change(true, this.ess.errTips(res.json()));
      }
    });
  }

}
